/* $Id: Actions.java 51 2010-01-11 00:54:55Z aaron.day $ */
package org.crazydays.gameplan;


import org.crazydays.gameplan.db.Database;
import org.crazydays.gameplan.db.event.CloseDatabase;
import org.crazydays.gameplan.db.event.CreateDatabase;
import org.crazydays.gameplan.db.event.OpenDatabase;
import org.crazydays.gameplan.db.event.SaveDatabase;
import org.crazydays.gameplan.db.event.SaveDatabaseAs;
import org.crazydays.gameplan.map.CreateMapDialog;
import org.crazydays.gameplan.map.event.ToggleGrid;
import org.crazydays.gameplan.model.IDatabaseObserver;
import org.crazydays.gameplan.swing.OpenAboutDialog;
import org.crazydays.gameplan.swing.ExitApplication;

/**
 * Actions
 */
public class Actions
    implements IDatabaseObserver
{
    /** serial version uid */
    public final static long serialVersionUID =
        "$Id: Actions.java 51 2010-01-11 00:54:55Z aaron.day $".hashCode();

    /** controller */
    protected Controller controller;

    /** create database */
    protected CreateDatabase createDatabase;

    /** open database */
    protected OpenDatabase openDatabase;

    /** close database */
    protected CloseDatabase closeDatabase;

    /** save database */
    protected SaveDatabase saveDatabase;

    /** save database as */
    protected SaveDatabaseAs saveDatabaseAs;

    /** create map dialog */
    protected CreateMapDialog createMapDialog;

    /** toggle grid */
    protected ToggleGrid toggleGrid;

    /** about */
    protected OpenAboutDialog about;

    /** exit */
    protected ExitApplication exit;

    /**
     * Actions constructor.
     * 
     * @param controller Controller
     */
    public Actions(Controller controller)
    {
        this.controller = controller;
    }

    /**
     * Initialize actions.
     */
    protected void initialize()
    {
        initializeActions();
        initializeState();
    }

    /**
     * Initialize actions.
     */
    protected void initializeActions()
    {
        // database
        createDatabase = new CreateDatabase(controller);
        openDatabase = new OpenDatabase(controller);
        closeDatabase = new CloseDatabase(controller);
        saveDatabase = new SaveDatabase(controller);
        saveDatabaseAs = new SaveDatabaseAs(controller);

        // map
        createMapDialog = new CreateMapDialog(controller);

        // view
        toggleGrid = new ToggleGrid(controller);

        // help
        about = new OpenAboutDialog(controller);

        // exit
        exit = new ExitApplication();
    }

    /**
     * Initialize state.
     */
    protected void initializeState()
    {
        closeDatabase.setEnabled(false);
        saveDatabase.setEnabled(false);
        saveDatabaseAs.setEnabled(false);
        toggleGrid.setEnabled(false);
        createMapDialog.setEnabled(false);
    }

    /**
     * Get new database action.
     * 
     * @return Action
     */
    public CreateDatabase getCreateDatabase()
    {
        return createDatabase;
    }

    /**
     * Get open database action.
     * 
     * @return Action
     */
    public OpenDatabase getOpenDatabase()
    {
        return openDatabase;
    }

    /**
     * Get close database action.
     * 
     * @return Action
     */
    public CloseDatabase getCloseDatabase()
    {
        return closeDatabase;
    }

    /**
     * Get save database action.
     * 
     * @return Action
     */
    public SaveDatabase getSaveDatabase()
    {
        return saveDatabase;
    }

    /**
     * Get save database as action.
     * 
     * @return Action
     */
    public SaveDatabaseAs getSaveDatabaseAs()
    {
        return saveDatabaseAs;
    }

    /**
     * Get create map dialog action.
     * 
     * @return Action
     */
    public CreateMapDialog getCreateMapDialog()
    {
        return createMapDialog;
    }

    /**
     * Get grid toggle action.
     * 
     * @return Action
     */
    public ToggleGrid getToggleGrid()
    {
        return toggleGrid;
    }

    /**
     * Get about action.
     * 
     * @return Action
     */
    public OpenAboutDialog getAbout()
    {
        return about;
    }

    /**
     * Get exit action.
     * 
     * @return Action
     */
    public ExitApplication getExit()
    {
        return exit;
    }

    /**
     * Database is opened.
     * 
     * @param database Database
     * @see IDatabaseObserver#opened(Database)
     */
    @Override
    public void opened(Database database)
    {
        createDatabase.setEnabled(false);
        openDatabase.setEnabled(false);
        saveDatabase.setEnabled(true);
        saveDatabaseAs.setEnabled(true);
        createMapDialog.setEnabled(true);
        closeDatabase.setEnabled(true);
    }

    /**
     * Database is closed.
     * 
     * @param database Database
     * @see IDatabaseObserver#closed(Database)
     */
    @Override
    public void closed(Database database)
    {
        createDatabase.setEnabled(true);
        openDatabase.setEnabled(true);
        saveDatabase.setEnabled(false);
        saveDatabaseAs.setEnabled(false);
        createMapDialog.setEnabled(false);
        closeDatabase.setEnabled(false);
    }

    /**
     * Enable map options.
     */
    public void enableMapOptions()
    {
        toggleGrid.setEnabled(true);
    }

    /**
     * Disable map options.
     */
    public void disableMapOptions()
    {
        toggleGrid.setEnabled(false);
    }
}
